using UnityEngine;

public class CameraController : SingletonMonoBehaviour<CameraController>
{
    public float _minSpeed;
    public float _percent;
    /// <summary>
    /// The distance between main character and the camera
    /// </summary>
    public Vector3 _photographyDistance;
    private GameObject _target;

    private void Awake()
    {
        INI_Singleton();
        //DontDestroyOnLoad(this);
    }

    private void Start()
    {
        _target = MainRole.Instance.gameObject;
    }

    private void Update()
    {
        if (_target == null) _target = MainRole.Instance.gameObject;
        CameraMove(_target.transform.position + _photographyDistance, _percent);
    }

    //private void CameraMove(Vector3 destination, float minSpeed)
    //{
    //    Vector3 distance = destination - gameObject.transform.position;
    //    gameObject.transform.position = Math.Abs(distance.magnitude) < 0.01 * minSpeed ? destination : gameObject.transform.position;//anti shaking
    //    _RB.velocity = distance.magnitude != 0 ?
    //        distance.magnitude < minSpeed ? distance.normalized * minSpeed : distance
    //        : Vector3.zero;
    //}

    /// <summary>
    /// Camera move on the XZ plat.
    /// </summary>
    /// <param name="destination"></param>
    /// <param name="percent"></param>
    private void CameraMove(Vector3 destination, float percent)
    {
        gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, destination, percent * Time.deltaTime);
    }

    public void ChangeTarget(GameObject target)
    {
        _target = target;
    }
}
